package com.aaa.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.dao.UserDao;
import com.aaa.utils.BaseDao;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/8/9 9:17
 * @description：用户dao实现类
 * @modified By：
 * @version: 1.0
 */
public class UserDaoImpl  extends BaseDao  implements UserDao {
    @Override
    public List<Map<String, Object>> queryAll(Map map) {
        StringBuffer sql=new StringBuffer("select * from tbl_user inner  join dept on deptid=deptno where 1=1 ");
        if(ObjectUtil.isNotEmpty(map)){
            //拼接查询条件
            if(ObjectUtil.isNotEmpty(map.get("username"))){
                sql.append(" and username like '%"+map.get("username")+"%' ");
            }
            if(ObjectUtil.isNotEmpty(map)&&ObjectUtil.isNotEmpty(map.get("deptid"))){
                sql.append(" and deptid="+map.get("deptid"));
            }
            //拼接排序的sql
            if(ObjectUtil.isNotEmpty(map)&& ObjectUtil.isNotEmpty(map.get("field"))){
                sql.append( " order by  "+map.get("field")+" "+map.get("order")+" ");
            }else{
                //默认按照userid倒序排列
                sql.append( " order by  userid desc");
            }
            //拼接分页的sql
            if(ObjectUtil.isNotEmpty(map)&& ObjectUtil.isNotEmpty(map.get("page"))){
                int page=  Integer.parseInt(map.get("page")+"");
                int limit=  Integer.parseInt(map.get("limit")+"");
                int start=(page-1)*limit;
                sql.append( " limit "+start+", "+limit);
            }
        }else
        {
            //默认按照userid倒序排列
            sql.append( " order by  userid desc");
        }
        System.out.println("sql："+sql);
        return executeQuery(sql.toString());
    }

    @Override
    public Long queryCount(Map map) {
        StringBuffer sql=new StringBuffer("select count(*) mycount from tbl_user where 1=1 ");
        //拼接查询条件
        if(ObjectUtil.isNotEmpty(map)&&ObjectUtil.isNotEmpty(map.get("username"))){
            sql.append(" and username like '%"+map.get("username")+"%' ");
        }
        if(ObjectUtil.isNotEmpty(map)&&ObjectUtil.isNotEmpty(map.get("deptid"))){
            sql.append(" and deptid="+map.get("deptid"));
        }
        List<Map<String, Object>> mapList = executeQuery(sql.toString());
        return Long.parseLong(mapList.get(0).get("mycount")+"");
    }

    @Override
    public boolean saveUser(Map map) {
        String sql="insert into tbl_user (username,password,salt,deptid,avatar,createtime) values (?,?,?,?,?,?)";
        return executeUpdate(sql,map.get("username"),map.get("password"),map.get("salt"),map.get("deptid"),map.get("avatar"),new Date());
    }

    @Override
    public boolean deleteUser(Map map) {
        String sql="delete from tbl_user where userid in ("+map.get("id")+")";
        return executeUpdate(sql);
    }

    @Override
    public List<Map<String, Object>> queryUserByName(Map map) {
        String sql ="select * from tbl_user where username =?";
        return executeQuery(sql,map.get("username"));
    }
}
